package com.wzdigit.wms.basic.service.code;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzdigit.framework.Shift;
import com.wzdigit.wms.basic.domain.mapping.CodeMapping;
import com.wzdigit.wms.basic.mapper.core.mapping.CodeMappingMapper;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 内外部系统代码映射表 服务实现类
 * 
 * @author Administrator
 *
 */
@Service
public class CodeMappingService {

	@Autowired
	private CodeMappingMapper codeMappingMapper;

	/**
	 * 根据工厂、仓库CODE
	 * 
	 * @param factory
	 * @param whsCode
	 * @param qaStatus
	 * @return
	 */
	public CodeMapping selectByWhsCodeAndFactoryOrQaStatus(String factory, String whsCode, Integer qaStatus) {
		if(StringUtils.isBlank(factory)) {
			Shift.fatal("工厂代码为空！");
		}
		if(StringUtils.isBlank(whsCode)) {
			Shift.fatal("仓库CODE为空！");
		}
		QueryWrapper<CodeMapping> wrapper = new QueryWrapper<>();
		wrapper.eq("FAC_CODE", factory);
		wrapper.eq("WMS_CODE", whsCode);
 		wrapper.eq(!(qaStatus == null), "QA_STATUS", qaStatus);
		List<CodeMapping> list = codeMappingMapper.selectList(wrapper);
		if(list.size() > 0) {
			return list.get(0);
		} else {
			Shift.fatal(String.format("根据仓库CODE:【%s】和工厂代码:【%s】未找SAP对应的仓库信息,请维护【内外部系统代码映射表】", whsCode, factory));
			return null;
		}
	}

	/**
	 * 根据映射代码、工厂代码
	 * 
	 * @param mappingCode
	 * @param factory
	 * @return
	 */
	public CodeMapping selectByMappingCodeAndFacCode(String mappingCode, String factory) {
		if(StringUtils.isBlank(mappingCode)) {
			Shift.fatal("映射代码为空！");
		}
		if(StringUtils.isBlank(factory)) {
			Shift.fatal("工厂代码为空！");
		}
		QueryWrapper<CodeMapping> wrapper = new QueryWrapper<>();
		wrapper.eq("CODE_TYPE", "whs_code");
		wrapper.eq("MAPPING_SYSTEM", "SAP");
		wrapper.eq("MAPPING_CODE", mappingCode);
		wrapper.eq("FAC_CODE", factory);
		List<CodeMapping> list = codeMappingMapper.selectList(wrapper);
		if(list.size() > 0) {
			return list.get(0);
		} else {
			Shift.fatal(String.format("根据映射代码:【%s】和工厂代码:【%s】未找SAP对应的仓库信息,请维护【内外部系统代码映射表】", mappingCode, factory));
			return null;
		}
	}

	/**
	 * 根据映射代码
	 * 
	 * @param mappingCode
	 * @return
	 */
	public CodeMapping selectByMappingCode(String mappingCode) {
		if(StringUtils.isBlank(mappingCode)) {
			return null;
			//Shift.fatal("映射代码为空！");
		}
		QueryWrapper<CodeMapping> wrapper = new QueryWrapper<>();
		wrapper.eq("CODE_TYPE", "whs_code");
		wrapper.eq("MAPPING_SYSTEM", "SAP");
		wrapper.eq("MAPPING_CODE", mappingCode);
		List<CodeMapping> list = codeMappingMapper.selectList(wrapper);
		if(list.size() > 0) {
			return list.get(0);
		} else {
			//Shift.fatal(String.format("根据映射代码:【%s】未找SAP对应的仓库信息,请维护【内外部系统代码映射表】", mappingCode));
			return null;
		}
	}
}
